package com.yyk.yiliao.config;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.hyphenate.easeui.EaseUI;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.yyk.yiliao.R;
import com.yyk.yiliao.cache.hx.HxHelper;
import com.yyk.yiliao.runtimepermissions.PermissionsManager;
import com.yyk.yiliao.runtimepermissions.PermissionsResultAction;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2018/6/14 0014.
 * 做一个有高逼咯的马龙
 */

public class MyApplication extends Application {

    private static final String TAG = "APPLOG";
    public static MyApplication application;
    private List<Activity> list;
    private Context applicationContext;

    public static MyApplication getInstance() {
        return application;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        application = this;

        applicationContext = this;
        list = new ArrayList<>();
        initHawk();
        initLogger(TAG);
        initYouMeng();
        initHuanXin();


    }

    public void addActivity(Activity activity) {
        if (!list.contains(activity)) {
            list.add(activity);
        }
    }

    public void removeActivity(Activity activity) {
        if (list.contains(activity)) {
            list.remove(activity);
        }
    }

    public void destroyActivity() {
        for (Activity activity : list) {
            if (list.contains(activity)) {
                list.remove(activity);
                activity.finish();
            }
        }
        System.exit(0);
    }

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.white, android.R.color.black);//全局设置主题颜色
                layout.setDisableContentWhenRefresh(false);//是否在刷新的时候禁止列表的操作
                layout.setDragRate(0.5f);//显示下拉高度/手指真实下拉高度=阻尼效果
                layout.setReboundDuration(300);//回弹动画时长（毫秒）

                layout.finishRefresh(500);//延迟3000毫秒后结束刷新
                return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
    }


    private void initYouMeng() {
        //注册有盟
        UMShareAPI.get(this);//初始化sdk
        /**
         * 设置组件化的Log开关
         * 参数: boolean 默认为false，如需查看LOG设置为true
         */
        UMConfigure.setLogEnabled(true);

        /**设置日志加密
         * 参数：boolean 默认为false（不加密）
         */
        UMConfigure.setEncryptEnabled(true);
        /**
         * 如果在注册清单里 声明APP KEY,就可以使用这个初始化方法
         *
         * 初始化common库
         * 参数1:上下文，不能为空
         * 参数2:APP_KEY  如果设置为null  在注册清单 添加   <meta-data    android:name="UMENG_APPKEY"   android:value="5ac6d250f43e4835e8000a27" />
         * 参数3:Channel  如果设置为null   同上
         * 参数4:设备类型，UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子，默认为手机
         * 参数5:Push推送业务的secret 需要集成Push功能必须传入的sercet,否则为空
         */
        UMConfigure.init(getApplicationContext(), "5b5e69b1b27b0a68120000da", "umeng", UMConfigure.DEVICE_TYPE_PHONE, "bd80dc15db26d33544e7203d9ba3ac3a");
        PlatformConfig.setWeixin("wx3fc8cda9570ff74d", "5fe513f153f35fb3b61c8e9681bf619f");
        //各个平台的配置，建议放在全局Application或者程序入口

    }


    /**
     * 初始化 Sp存储
     */
    private void initHawk() {
        Hawk.init(this).build();
    }


    /**
     * 初始化Logger
     *
     * @param tag
     */
    private void initLogger(String tag) {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // 是否显示线程信息，默认 显示
                .methodCount(0)         // 方法栈打印的个数，默认是 2
                .methodOffset(7)        // 设置调用堆栈的函数偏移值，默认是 5
                .logStrategy(null) // 设置log打印策略，默认是 LogCat
                .tag(tag)   // 设置全局TAG，默认是 PRETTY_LOGGER
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return true;
            }
        });


    }

    /**
     * 环信配置
     */

    private void initHuanXin() {
        HxHelper.getInstance().init(applicationContext);
    }



}
